﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using BizProxy.Models;
using Creative85.Framework.Data;
using Creative85.Framework.Utilities;
using Project32CHE.Biz.Unionshop;
using Project32CHE.Biz.Unionshop.Models;
using Project32CHE.Biz.UserAccount;
using Project32CHE.Biz.UserAccount.Models;

namespace BizProxy
{
    public static class AccountProxyService
    {
        public readonly static string BaseUrl = ConfigurationManager.AppSettings["ServiceBaseUrl"] + "Account";

        public static bool ExistAccount(string account)
        {
            var result = true;
            if (!Project32CHE.Biz.UserAccount.AccountService.Exist(account))
            {
                var json = RequestHelper.PostJson<object>(BaseUrl + "/ExistAccount", new
                {
                    account = account
                });
                switch (json.code)
                {
                    case 0: break;
                    case 1000: result = false; break;
                    default:
                        throw new NotSupportedException(json.msg);
                }
            }
            return result;
        }
        

        /// <summary>
        /// 获取登录地址
        /// </summary>
        /// <returns></returns>
        public static string GetLoginUrl()
        {
            return BaseUrl + "/Login";
        }

        /// <summary>
        /// 验证token
        /// </summary>
        /// <param name="token"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool Validate(string token, out User user)
        {
            user = null;
            var para = new Dictionary<string, string>()
            {
                {"token",token}
            };
            var ret = RequestUtility.HttpPost(BaseUrl + "/LoginValidate", para);
            var result = !string.IsNullOrEmpty(ret);
            if (result)
            {
                var userId = Guid.Empty;
                result = Guid.TryParse(ret, out userId);
                if (result)
                {
                    user = UserService.Get(userId);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取退出地址
        /// </summary>
        /// <returns></returns>
        public static string GetLogoutUrl()
        {
            return BaseUrl + "/Logout";
        }
    }
}
